﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Icinetic.FeedCategorySample.Common;
using Icinetic.FeedCategorySample.Entities;
using Windows.Data.Xml.Dom;
using Windows.UI.Notifications;
using Windows.Web.Syndication;
using Icinetic.FeedCategorySample.Services;
using Icinetic.FeedCategorySample.Common.Tiles;
using System.Collections.ObjectModel;

namespace Icinetic.FeedCategorySample.ViewModel
{
    public class MainPageViewModel : BindableBase
    {
        private string _title = "Rss Power Template";
        public string Title
        {
            get { return _title; }
            set { SetProperty(ref _title, value); }
        }

        private MainPageStateEnum _state = MainPageStateEnum.ZoomInMenu;
        public MainPageStateEnum State
        {
            get { return _state; }
            set { SetProperty(ref _state, value); }
        }

        private ObservableCollection<Category> _categoryList = new ObservableCollection<Category>();
        public virtual ObservableCollection<Category> CategoryList
        {
            get { return this._categoryList; }
            set { SetProperty(ref _categoryList, value); }
        }

        private string _logoPortada = "Assets/LandscapeLogo.png";
        public virtual string LogoPortada
        {
            get { return this._logoPortada; }
            set { this._logoPortada = value; }
        }

        private bool _isloading = true;
        public bool IsLoading
        {
            get { return _isloading; }
            set { SetProperty(ref _isloading, value); }
        }

        public MainPageViewModel()
        {

        }

        public async Task LoadFeedList()
        {
            IsLoading = true;

            Dictionary<string, List<RssItem>> feedDictionary = new Dictionary<string, List<RssItem>>();

            //OneURL
            ContentService contentService = new ContentService(App.FeedUrl);
            await contentService.GetItems<RssItem>(feedDictionary);
            //End OneURL

            //MultipleURL
            //Dictionary<string, string> urlDictionary = new Dictionary<string, string>();
            //urlDictionary.Add("NombreCategoria", "Url");
            //ContentService contentService = new ContentService(urlDictionary);
            //await contentService.GetItemsMultipleUrl<RssItem>(feedDictionary);
            //End MultipleURL

            List<Category> categoryList = LoadCategories(feedDictionary);
            ConvertCategoryList(categoryList);

            UpdateTiles(feedDictionary);

            IsLoading = false;
        }

        private void UpdateTiles(Dictionary<string, List<RssItem>> feedDictionary)
        {
            new TilesHelper().Publish(feedDictionary, "Assets/noimage-hor.png");
        }

        private List<Category> LoadCategories(Dictionary<string, List<RssItem>> feedDictionary)
        {
            List<Category> categoryList = new List<Category>();

            int colorIndex = 0;
            foreach (var categoryItem in feedDictionary)
            {
                LoadItems(categoryItem.Value);

                Category category = new Category()
                {
                    Title = categoryItem.Key,
                    ColSpan = 2,
                    RowSpan = 1,
                    Image = "Assets/FondoPortrait.scale-100.png",
                    Color = GetTileColor(colorIndex++),
                    Articles = ConvertRssItemList(categoryItem.Value)
                };
                categoryList.Add(category);
            }

            return categoryList;
        }

        private void LoadItems(List<RssItem> itemList)
        {
            int i = 1, j = 1;
            foreach (RssItem rss in itemList)
            {
                if (itemList.Count == 1)
                {
                    rss.ColSpan = 5;
                    rss.RowSpan = 3;
                    rss.ColPortraitSpan = 3;
                    rss.RowPortraitSpan = 5;
                }
                else
                {
                    ColumnRowCalculate(rss, ref i);
                    ColumnRowPortraitCalculate(rss, ref j);
                }
            }
        }

        private void ConvertCategoryList(List<Category> categoryList) 
        {
            ObservableCollection<Category> auxList = new ObservableCollection<Category>();
            foreach (Category category in categoryList)
                auxList.Add(category);
           CategoryList = auxList;
        }

        private ObservableCollection<RssItem> ConvertRssItemList(List<RssItem> categoryList)
        {
            ObservableCollection<RssItem> auxList = new ObservableCollection<RssItem>();
            foreach (RssItem category in categoryList)
                auxList.Add(category);
            return auxList;
        }

        private void ColumnRowCalculate(RssItem feed, ref int i)
        {
            int colSpan = 0, rowSpan = 0;
            switch(i)
                {
                    case 1:
                        rowSpan = 2;
                        colSpan = 4;
                        break;
                    case 2:
                        rowSpan = 1;
                        colSpan = 2;
                        break;
                    case 3:
                        rowSpan = 1;
                        colSpan = 2;
                        break;
                    case 4:
                        rowSpan = 3;
                        colSpan = 2;
                        break;
                    case 5:
                        rowSpan = 1;
                        colSpan = 2;
                        break;
                    case 6:
                        rowSpan = 1;
                        colSpan = 2;
                        break;
                    case 7:
                        rowSpan = 1;
                        colSpan = 2;
                        break;
                }

            if (i == 7)
                i = 1;
            else
                i++;
           
            feed.RowSpan = rowSpan;
            feed.ColSpan = colSpan;
        }

        private void ColumnRowPortraitCalculate(RssItem feed, ref int j)
        {
            int colPSpan = 0, rowPSpan = 0;
            switch (j)
            {
                case 1:
                    rowPSpan = 3;
                    colPSpan = 2;
                    break;
                case 2:
                    rowPSpan = 2;
                    colPSpan = 4;
                    break;
                case 3:
                    rowPSpan = 1;
                    colPSpan = 2;
                    break;
                case 4:
                    rowPSpan = 1;
                    colPSpan = 2;
                    break;
                case 5:
                    rowPSpan = 1;
                    colPSpan = 2;
                    break;
                case 6:
                    rowPSpan = 2;
                    colPSpan = 4;
                    break;
                case 7:
                    rowPSpan = 1;
                    colPSpan = 2;
                    break;
                case 8:
                    rowPSpan = 2;
                    colPSpan = 4;
                    break;
                case 9:
                    rowPSpan = 1;
                    colPSpan = 2;
                    break;
            }

            if (j == 9)
                j = 1;
            else
                j++;

            feed.RowPortraitSpan = rowPSpan;
            feed.ColPortraitSpan = colPSpan;
        }

        public static string GetTileColor(int i)
        {
            List<string> colors = new List<string>() { "#0099AB", "#DB562E", "#A400AB", "#00A400", "#BD1E4A", "#613CBC", "#7F0000" };
            return colors[i % colors.Count()];
        }
    }

    public enum MainPageStateEnum
    { 
        ZoomInMenu = 1,
        ZoomOutMenu = 2,
        Cover = 3
    }
}
